//
//  EditWorkInfoViewController.m
//  HuaRun2
//
//  Created by admin on 2017/11/2.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "EditWorkInfoViewController.h"
#import "LabelTextfieldView.h"
#import "LineView.h"
#import "ChooseTopPersonViewController.h"
#import "Contact_ListModel.h"
#import "Contact_DetailModel.h"

@interface EditWorkInfoViewController ()
@property(nonatomic, strong)UIScrollView * scrollView;
//助理
@property(nonatomic, strong)LabelTextfieldView * zhuLiLF;
//助理电话
@property(nonatomic, strong)LabelTextfieldView * zhuLiTelLF;
//助理私人电话
@property(nonatomic, strong)LabelTextfieldView * zhuLiPriTelLF;

//汇报人
@property(nonatomic, strong)UIView * huiBaoRenView;
//汇报人 的value
@property(nonatomic, strong)UILabel * huiBaoRenValueLab;

//助理邮箱
@property(nonatomic, strong)LabelTextfieldView * zhuLiEmailLF;
//主管
@property(nonatomic, strong)LabelTextfieldView * zhuGuanLF;
//下属
@property(nonatomic, strong)LabelTextfieldView * xiaShuLF;
@property(nonatomic, strong)Contact_ListModel * personModel;
@end

@implementation EditWorkInfoViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    _scrollView.contentSize = CGSizeMake(kMainScreenWidth, CGRectGetMaxY(self.xiaShuLF.frame) + 10);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)viewInit {
    self.navigationItem.title = NSLocalizedString(@"编辑联系人工作信息", nil);
    self.view.backgroundColor = [UIColor whiteColor];
    
    [super viewInit];
    
    [self.view addSubview:self.scrollView];
    
    [self.scrollView addSubview:self.zhuLiLF];
    [self.scrollView addSubview:self.zhuLiTelLF];
    [self.scrollView addSubview:self.zhuLiPriTelLF];
    
    self.huiBaoRenView = [self makeShengRiView];
    [self.scrollView addSubview:self.huiBaoRenView];
    
    [self.scrollView addSubview:self.zhuLiEmailLF];
    [self.scrollView addSubview:self.zhuGuanLF];
    [self.scrollView addSubview:self.xiaShuLF];
    
    weakself(ws);
    [self nbRightImageName:@"save" title:nil taget:self clickBlock:^(id result) {
        NSLog(@"....完成");
        [ws submitMethod];
    }];
    [self configureInfo];
}

- (void)layoutInit {
    [super layoutInit];
    
    weakself(ws);

    [self.zhuLiLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.scrollView).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    [self.zhuLiTelLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.zhuLiLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    [self.zhuLiPriTelLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.zhuLiTelLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    [self.zhuLiEmailLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.zhuLiPriTelLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.huiBaoRenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.zhuLiEmailLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
        make.width.mas_equalTo(kMainScreenWidth);
        make.height.mas_equalTo(35);
    }];

    [self.zhuGuanLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.huiBaoRenView.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.xiaShuLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.zhuGuanLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];

}
#pragma mark 方法
- (void)configureInfo {
    _zhuLiLF.textField.text = [NSString showSpaceStringWithStr: _detailModel.assistantNameC];
    
    _zhuLiTelLF.textField.text = [NSString showSpaceStringWithStr: _detailModel.assistantTel];
    _zhuLiPriTelLF.textField.text = [NSString showSpaceStringWithStr:_detailModel.assistantMobile];
    _zhuLiEmailLF.textField.text = [NSString showSpaceStringWithStr:_detailModel.assistantEmail];
    
    _huiBaoRenValueLab.text  =[NSString showSpaceStringWithStr: _detailModel.reportToName];
    _zhuGuanLF.textField.text  =[NSString showSpaceStringWithStr: _detailModel.prNameOfSupervisor];
    _xiaShuLF.textField.text  =[NSString showSpaceStringWithStr: _detailModel.prNameOfSubordinate];
}

- (void)submitMethod {
    [self.view endEditing:YES];

    NSString * zhuLiName = [NSString showStringWithStr:_zhuLiLF.textField.text];
    NSString * zhuLiTel =  [NSString showStringWithStr:_zhuLiTelLF.textField.text];
    NSString * zhuLiPriTel =  [NSString showStringWithStr:_zhuLiPriTelLF.textField.text];
    
    NSString * zhuLiEmail = [NSString showStringWithStr:_zhuLiEmailLF.textField.text];
    
    NSString * huiBaoRenId = self.personModel.id;
    NSString * zhuGuan = [NSString showStringWithStr: _zhuGuanLF.textField.text];
    NSString * xiaShu = [NSString showStringWithStr: _xiaShuLF.textField.text];

//    "id":  联系人编号，必需,
//    "accountId": 公司编号，必需
//    "assistantEmail": 助理邮箱,
//    "assistantMobile": 助理移动电话,
//    "assistantNameC": 助理,
//    "assistantTel":助理电话,
//    "birthdayDay": 生日（日）,
//    "birthdayMonth": 生日（月）,
//    "birthdayYear": 生日（年）,
//    "cddCategory": "CDD标签",
//    "cddRemarks": CDD备注,
//    "chinaMobile": 内地手机,
//    "createById": 创建人编号,
//    "prNameOfSubordinate": 下属,
//    "prNameOfSupervisor": 主管,
//    "reportsToId": 汇报人编号,

    NSDictionary * finalDic = @{@"id": _contactId ? _contactId : @"",
                                @"accountId": _detailModel.accountId ? _detailModel.accountId : @"",
                                @"assistantNameC" : zhuLiName,
                                @"assistantTel" : zhuLiTel,
                                @"assistantMobile" : zhuLiPriTel,
                                @"assistantEmail" : zhuLiEmail,
                                @"prNameOfSubordinate" : xiaShu,
                                @"prNameOfSupervisor" : zhuGuan,
                                @"reportsToId" : huiBaoRenId};
    
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kEditContactUrl];
    
    weakself(ws);
    [self showActivity:NSLocalizedString(@"正在提交", nil)];
    
    [NetWorkHelper postWithURLString:url parameters:finalDic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        
        [ws showAlert:kEditSuccess completionBlock:^{
            [ws.navigationController popViewControllerAnimated:YES];
        }];
        
        
    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
}

//查看汇报人列表
- (void)seeHuiBaoRenDetail {
    ChooseTopPersonViewController * companyVC = [ChooseTopPersonViewController new];
    weakself(ws);

    companyVC.clickBlock = ^(id result) {
        ws.personModel = result;
         NSString * name = [NSString stringWithFormat:@"%@  %@",
         [NSString showSpaceStringWithStr: ws.personModel.firstName],
         [NSString showSpaceStringWithStr: ws.personModel.lastName]];

        ws.huiBaoRenValueLab.text = name;
    };
    [self.navigationController pushViewController:companyVC animated:YES];

}

#pragma mark - getter
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

//"zhuLi" = "助理";
- (LabelTextfieldView *)zhuLiLF {
    if (!_zhuLiLF) {
        self.zhuLiLF = [LabelTextfieldView new];
        _zhuLiLF.nameLab.text =NSLocalizedString(@"zhuLi", nil);
    }
    return _zhuLiLF;
}

//"zhuLiTel" = "助理电话";
- (LabelTextfieldView *)zhuLiTelLF {
    if (!_zhuLiTelLF) {
        self.zhuLiTelLF = [LabelTextfieldView new];
        _zhuLiTelLF.nameLab.text =NSLocalizedString(@"zhuLiTel", nil);
        _zhuLiTelLF.textField.keyboardType =UIKeyboardTypePhonePad;
    }
    return _zhuLiTelLF;
}

//"zhuLiPriTel" = "助理私人电话";
- (LabelTextfieldView *)zhuLiPriTelLF {
    if (!_zhuLiPriTelLF) {
        self.zhuLiPriTelLF = [LabelTextfieldView new];
        _zhuLiPriTelLF.nameLab.text =NSLocalizedString(@"zhuLiPriTel", nil);
        _zhuLiPriTelLF.textField.keyboardType =UIKeyboardTypePhonePad;
    }
    return _zhuLiPriTelLF;
}

//"zhuLiEmail" = "助理邮箱";
- (LabelTextfieldView *)zhuLiEmailLF {
    if (!_zhuLiEmailLF) {
        self.zhuLiEmailLF = [LabelTextfieldView new];
        _zhuLiEmailLF.nameLab.text =NSLocalizedString(@"zhuLiEmail", nil);
        _zhuLiEmailLF.textField.keyboardType =UIKeyboardTypeEmailAddress;
    }
    return _zhuLiEmailLF;
}

//"zhuGuan" = "主管";
- (LabelTextfieldView *)zhuGuanLF {
    if (!_zhuGuanLF) {
        self.zhuGuanLF = [LabelTextfieldView new];
        _zhuGuanLF.nameLab.text =NSLocalizedString(@"zhuGuan", nil);
    }
    return _zhuGuanLF;
}

//"xiaShu" = "下属";
- (LabelTextfieldView *)xiaShuLF {
    if (!_xiaShuLF) {
        self.xiaShuLF = [LabelTextfieldView new];
        _xiaShuLF.nameLab.text =NSLocalizedString(@"xiaShu", nil);
    }
    return _xiaShuLF;
}

//汇报人view
- (UIView *)makeShengRiView {
    UIView * guoJIView = [UIView new];
    //        guoJIView.backgroundColor = kRandomColor;
    
    weakself(ws);
    [guoJIView addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [ws seeHuiBaoRenDetail];
    }];
    UILabel * label = [UILabel new];
    label.textAlignment = NSTextAlignmentRight;
    label.font = kGeneralFontSize;
    label.textColor = [UIColor colorWithHex:@"3c3c3c"];
    label.text = NSLocalizedString(@"huiBaoRen", nil);
    [guoJIView addSubview:label];
    
    UILabel * valueLabel = [UILabel new];
    valueLabel.textAlignment = NSTextAlignmentLeft;
    valueLabel.font = kGeneralFontSize;
    valueLabel.textColor = [UIColor colorWithHex:@"3c3c3c"];
    //    valueLabel.backgroundColor = kRandomColor;
    [guoJIView addSubview:valueLabel];    
    self.huiBaoRenValueLab = valueLabel;
    
    UIButton * btn = [UIButton new];
    btn.frame = CGRectMake(kMainScreenWidth - 30, 10, 10, 10);
    btn.userInteractionEnabled = NO;
    [btn setBackgroundImage:[UIImage imageNamed:@"return-拷贝"]
                   forState:(UIControlStateNormal)];
//    [btn addTarget:self action:@selector(editWorkInfo) forControlEvents:(UIControlEventTouchUpInside)];
    [guoJIView addSubview:btn];

    LineView * line1 = [LineView new];
    [guoJIView addSubview:line1];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.centerY.mas_equalTo(valueLabel);
        make.right.mas_equalTo(valueLabel.mas_left).offset(-kMargin15);
    }];
    
    [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(guoJIView).offset(-kMargin15);
        make.centerY.mas_equalTo(guoJIView);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(guoJIView).multipliedBy(0.6);
    }];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(guoJIView).offset(kMargin10);
        make.right.mas_equalTo(guoJIView).offset(-kMargin10);
        make.height.mas_equalTo(1);
        make.bottom.mas_equalTo(guoJIView);
    }];
    
    return guoJIView;
}
@end
